home *** CD-ROM | disk | FTP | other *** search
/ Cracking 1 / Cracking I..iso / Tools / Debuggery / TRW2000 / PLUGSDK / BPINT / BPINT.CPP next >
Encoding:
C/C++ Source or Header  |  2000-03-10  |  6.2 KB  |  222 lines

  1. // BPINT Plugs for TRW2000 for Windows 9x
  2. // Copyright (C) , 1999-2000 ,
  3. //
  4. // Author:
  5. //     Zhunanhao, reached at nhzhu@163.net
  6. //
  7. // History :
  8. //    2000.2.23  Zhunanhao write origin code
  9. //
  10. // Note:
  11. //    This is only a DEMO ! And testing NOT completed, do NOT use BPINT 3, or it'll crash!
  12. //
  13. //    Please, DO NOT develop any breakpoint plug-ins now! Because we are
  14. //    developing breakpoint-system now, all interface of breakpoint-system 
  15. //    maybe changed!
  16. //
  17.  
  18. #include <wdm.h>
  19. #include "..\INCLUDE\PLUGS.H"
  20.  
  21. // prototypes
  22.  
  23. EXC NTSTATUS
  24. DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);
  25.  
  26. VOID
  27. PLUGS_Unload(IN PDRIVER_OBJECT DriverObject);
  28.  
  29.  
  30. NTSTATUS
  31. DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  32. {
  33.     NTSTATUS    ntStatus = STATUS_SUCCESS;
  34.  
  35.     DriverObject->DriverUnload = PLUGS_Unload;
  36.  
  37.     return ntStatus;
  38. }
  39.  
  40. VOID
  41. PLUGS_Unload(IN PDRIVER_OBJECT DriverObject)
  42. {
  43. }
  44.  
  45. /****************** WDM Rountine End ****************/
  46.  
  47. // prototype
  48.  
  49. BOOL cmd_BPINT( int argc,char** argv ) ;
  50.  
  51. // end
  52.  
  53. PLUGS_API* api = 0 ;
  54. // Call TRW2000 API must like this:
  55. //      api->Add_Command ( ) ;
  56.  
  57. EXC EXPORT BOOL Plugs_Init ( PLUGS_API* plugsapi)
  58. {
  59.     api=plugsapi;    
  60.  
  61.     api->Add_Command (    "BPINT" , "interrupt-number" ,
  62.                 "Breakpoint on interrupt" ,
  63.                 "BPINT 3\tBPINT 21",
  64.                 cmd_BPINT ) ;
  65.  
  66.     api->dprintf ( "BPINT Plugs Ver 0.01 Initialized..." ) ;
  67.  
  68.     return TRUE ;
  69. }
  70.  
  71. EXC EXPORT BOOL Plugs_Exit ( )
  72. {
  73.     return TRUE ;
  74. }
  75.  
  76. /***************** Command ***************/
  77.  
  78.  
  79. // Command : NPINT
  80. // NOTE:
  81. //    This is only a demo, it DO NOT call any breakpoint-system function of TRW2000, 
  82. //    so you can not use BC to clear it. It's not a real breakpoint that recognized
  83. //    by TRW2000. 
  84. //    It's too bad. But maybe it will be improved. We'll change the breakpoint-system
  85. //    of TRW2000. Export more    functions. 
  86. //
  87. //    Now use -R to clear it.
  88. //
  89.  
  90. WORD    IntBPid=0x1000;
  91.  
  92. BOOL    myIntBPhandle()
  93. {
  94.     if( *(api->pActive_BP_ID)!=IntBPid )
  95.         return FALSE;
  96.     // Does the action "Enter Kernel" caused by breakpoint?
  97.     // Is yes, Active_BP_ID include the id of breakpoint,
  98.     // or Active_BP_ID is 0xFFFF.
  99.  
  100.     api->unhook(api->hh_PreEnterKernel,myIntBPhandle);
  101.     api->DeleteBP((BYTE)IntBPid);
  102.     return FALSE;
  103.     // In the step "PreEnterKernel", you have a chance to prevent to enter kernel,
  104.     // if you return TRUE, TRW2000 will exit to kernel directly. Or return FALSE,
  105.     // TRW2000 will be actived.
  106. }
  107.  
  108. DWORD    HookIntNum=0xFFFFFFFF;
  109.  
  110. BOOL myinthandler(DWORD intnum)
  111. {
  112.     if( HookIntNum!=intnum )
  113.         return FALSE;
  114.         // Return FALSE means that put this interrupt to the next handler.
  115.         // If there is no next handler, TRW2000 will pass this interrupt
  116.         // to Windows.
  117.         
  118.     if( api->GetTraceFlag()==TRUE )
  119.         return FALSE;    
  120.     // what is GetTraceFlag? If user use F8(T) to trace program, this flag will be set,
  121.     // When user press F8, we can NOT capture the interrupt.
  122.     if( *(api->pfSoft_1Step)==TRUE )
  123.         return FALSE;
  124.     // what is fSoft_1Step? It means that some part of TRW2000( maybe a plug-ins) need that 
  125.     // the code debugged run one step then return to TRW2000. For example, breakpoint-system
  126.     // set this to set INT 3. In this state, we can NOT capture the interrupt too!
  127.     // The difference of fSoft1_Step and TraceFlag is TraceFlag is readable, fSoft_1Step is
  128.     // full-access. You can set it to do some special functions. But must be carefully, we
  129.     // recommend you e-mail us first to get advanced document, if you want to use fSoft_1Step.
  130.     
  131.     // Add your check proc here.
  132.     // e.g:
  133.     //    if( api->pUser->CRS.Client_EAX!=0x12345678 )
  134.     //        return FALSE;
  135.     // This means that if EAX of code debugged is not 0x12345678, TRW2000 will not be active.
  136.     //
  137.     // And, you can add your "DO" code here.
  138.     // e.g:
  139.     api->dprintf("Break due on interrupt %02X",intnum);
  140.     // This means print a line to command window.
  141.             
  142.     api->Set_Enter_Kernel();
  143.     // Active TRW2000!
  144.  
  145.     ADDRE    addr;
  146.     BYTE    opcode;
  147.     BYTE    opdata;
  148.     BOOL    fFault=FALSE;
  149.  
  150.     api->CurCSEIP(&addr);
  151.     addr._off--;
  152.     if( api->PeekB(&addr,&opdata)==FALSE )    // Can readable?
  153.         fFault=TRUE;
  154.     addr._off--;
  155.     if( api->PeekB(&addr,&opcode)==FALSE )    // Can readable?
  156.         fFault=TRUE;
  157.  
  158.     if( intnum==3 && opdata==0xcc && fFault==FALSE)    { // Is INT 3?
  159.         api->pUser->CRS.Client_EIP--;
  160.         return FALSE;
  161.     }
  162.         
  163.     if( opcode==0xcd && opdata==intnum && fFault==FALSE ) {    // Is INT xx?
  164.         api->pUser->CRS.Client_EIP-=2;
  165.         return FALSE;
  166.     }
  167.  
  168.     // Now, we need to handle hardware interrupt and fault interrupt, because
  169.     // in these status we can not see any code like "INT xx"
  170.     DWORD    intseg;
  171.     DWORD    intoff;
  172.     BOOL    gatesize;
  173.     api->Get_Origin_Interrupt_Vector(intnum,&intseg,&intoff,&gatesize);
  174.     addr._seg=(WORD)intseg;
  175.     addr._off=intoff;        
  176.     
  177.     IntBPid=api->addBPInt3(&addr,BP_IMMED);
  178.     api->hook(api->hh_PreEnterKernel,myIntBPhandle,0);
  179.     // Ok, we set a breakpoint on the entry point of Windows's ISRs,
  180.     // Now we can let's go!
  181.  
  182.     return FALSE;
  183.     // Return TRUE means that prevent TRW2000 to pass the interrupt to next handler    
  184.     // or Windows. It tell TRW2000 that the handling of interrupt has completed.
  185.     // When this function return, TRW2000 will check that whether need to active
  186.     // TRW2000. If yes, TRW2000 will pop-up. If no, there is nothing will be happened.
  187.     // Why we return FALSE here?
  188.     // Because we need to pass the interrupt to Windows! We can't handle INT21H AH=3FH!    
  189. }
  190.  
  191. BOOL cmd_BPINT( int argc,char**argv )
  192. {
  193.     DWORD    Intnum;
  194.     if( argc!=1 )
  195.         return FALSE;
  196.     
  197.     if( api->strcmp(argv[0],"-R")==0 ) {
  198.         api->unhook(api->hh_IntObj+HookIntNum*4,myinthandler);
  199.         // Remove the interrupt handler. TRW2000 will modify IDT and restore
  200.         // the origin interrupt vector.
  201.         // Ues API api->UpdateIDT() to make the modification done immediately.        
  202.         HookIntNum=0xFFFFFFFF;
  203.         return TRUE;
  204.     }
  205.     
  206.     if( HookIntNum!=0xFFFFFFFF ) {
  207.         api->dprintf("BPINT only can be use once.");
  208.         return TRUE;
  209.     }                
  210.         
  211.     PSTR s=api->str2hex(argv[0],&Intnum);
  212.     if( *s!=0 )
  213.         return FALSE;
  214.  
  215.     HookIntNum=Intnum;        
  216.     api->hook(api->hh_IntObj+HookIntNum*4,myinthandler,0);
  217.     // Add a interrupt handler. TRW2000 will modify IDT
  218.     // and hook this interrupt.
  219.     // Ues API api->UpdateIDT() to make the modification done immediately.        
  220.  
  221.     return TRUE;
  222. }